// 云函数入口文件
const cloud = require('wx-server-sdk')
const TcbRouter = require("tcb-router")
const axios = require("axios")
const baseUrl = "http://47.94.210.129:3002/"

cloud.init({
    env: "malu-9gjr8vrn08c2dfb4"
}) // 使用当前云环境

const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
    let app = new TcbRouter({
        event
    })
    app.router("getPlayListData", async (ctx, next) => {
        ctx.body = await db.collection("playList").skip(event.start).limit(event.count).orderBy("createTime", "asc").get().then(res => {
            return res.data;
        })
    })
    app.router("musicList", async (ctx, next) => {
        let response = await axios.get(baseUrl + "playList/detail?id=" + parseInt(event.playListId))
        console.log("response:", response);
        ctx.body = response.data;
    })
    app.router("songURL", async (ctx, next) => {
        ctx.body = await axios.get(baseUrl + "song/url?id=" + event.id).then(response => {
            return response.data;
        })
    })
    app.router("lyrics", async (ctx, next) => {
        ctx.body = await axios.get(baseUrl + "lyric?id=" + event.id).then(response => {
            return response.data;
        })
    })

    return app.serve();
}